<?php
/**
 * 公司
 */

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Model\RoleModel;

class RoleController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->middleware('auth');
    }

    /** 角色列表页面 */
    public function listRole()
    {
        $this->data['title'] = '角色列表';
        $this->showSidebar();
        return view('backend.role.listrole', $this->data);
    }

    /** 权限列表页面 */
    public function listAuthority()
    {

    }

    /** 列表角色数据 */
    public function listRolePost()
    {
        $backendUser = Auth::user();
        if ($backendUser->backend_group_id == 0) {
            $total = 0;
            $list = [];
        } else {
            $where = ['group_id' => $backendUser->backend_group_id];
            if (isset(self::$params['keyword']) && !empty(self::$params['keyword'])) {
                $where[] = ['role.role_name', 'like', '%' . self::$params['keyword'] . '%'];
            }

            if (isset(self::$params['status']) && self::$params['status'] != -1) {
                $where[] = ['role.status', '=', self::$params['status']];
            }

            $offset = (self::$params['page_num'] - 1) * self::$params['page_size'];
            $total = RoleModel::where($where)->count();
            $list = RoleModel::where($where)->offset($offset)->limit(self::$params['page_size'])->get()->toArray();
        }

        return $this->responseSuccess('成功', ['total' => $total, 'list' => $list]);
    }

    /** 添加角色 */
    public function addRolePost()
    {
        $backendUser = Auth::user();
        $data = [
            'role_name' => self::$params['role_name'],
            'group_id' => $backendUser->backend_group_id,
            'admin_id' => $backendUser->id,
            'status' => self::$params['status'],
            'createtime' => time(),
            'note' => self::$params['note']
        ];

        if (RoleModel::insert($data)) {
            return $this->responseSuccess();
        } else {
            return $this->responseError();
        }
    }

}
